package com.tom;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class MemberController
 */
public class ProductController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String LIST_PRODUCT = "listProduct.jsp";
	private static final String ADD_PRODUCT = "product.jsp";
	private static final String EDIT_PRODUCT = "product_edit.jsp";

	private ProductDAO dao;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ProductController() {
		super();
		dao = new ProductDAO();
	}


	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
//		request.setCharacterEncoding("UTF-8");
//		String forward = LIST_PRODUCT;
//		String action = request.getParameter("action");
//		if (action == null) {
//			forward = LIST_PRODUCT;
//		} else {
//			if (action.equalsIgnoreCase("listproduct")) {
//				forward = LIST_PRODUCT;
//			}
//			if (action.equalsIgnoreCase("addproduct")) {
//				if (request.getParameter("p_number") != null) {
//					// save
//					String p_number = request.getParameter("p_number");
//					String p_desc = request.getParameter("p_desc");
//					String p_name = request.getParameter("p_name");
//					String p_class = request.getParameter("p_class");
//					int price = Integer.parseInt(request.getParameter("price"));
//					int selling = Integer.parseInt(request
//							.getParameter("selling"));
//					int stock_qty = Integer.parseInt(request
//							.getParameter("stock_qty"));
//					int loan_qty = Integer.parseInt(request
//							.getParameter("loan_qty"));
//					int usable = Integer.parseInt(request
//							.getParameter("usable"));
//					String maping = request.getParameter("maping");
//					Product p = new Product(p_number, p_desc, p_name, p_class,
//							price, selling, stock_qty, loan_qty, usable, maping);
//					dao.add(p);
//					forward = LIST_PRODUCT;
//				} else {
//					forward = ADD_PRODUCT;
//				}
//				}
//				if (action.equalsIgnoreCase("edit")) {
//
//					Product p = dao.getProductById(request
//							.getParameter("p_number"));
//					request.setAttribute("p", p);
//					forward = EDIT_PRODUCT;
//
//				}
//		}
//				List<Product> products = dao.getAll();
//				request.setAttribute("products", products);
//				RequestDispatcher dispatcher = request
//						.getRequestDispatcher(forward);
//				dispatcher.forward(request, response);
			}
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String forward = LIST_PRODUCT;
		String action = request.getParameter("action");
		if (action == null) {
			forward = LIST_PRODUCT;
		} else {
			if (action.equalsIgnoreCase("listproduct")) {
				forward = LIST_PRODUCT;
			}
			if (action.equalsIgnoreCase("addproduct")) {
				if (request.getParameter("p_number") != null) {
					// save
					String p_number = request.getParameter("p_number");
					String p_desc = request.getParameter("p_desc");
					String p_name = request.getParameter("p_name");
					String p_class = request.getParameter("p_class");
					int price = Integer.parseInt(request.getParameter("price"));
					int selling = Integer.parseInt(request
							.getParameter("selling"));
					int stock_qty = Integer.parseInt(request
							.getParameter("stock_qty"));
					int loan_qty = Integer.parseInt(request
							.getParameter("loan_qty"));
					int usable = Integer.parseInt(request
							.getParameter("usable"));
					String maping = request.getParameter("maping");
					Product p = new Product(p_number, p_desc, p_name, p_class,
							price, selling, stock_qty, loan_qty, usable, maping);
					dao.add(p);
					forward = LIST_PRODUCT;
				} else {
					forward = ADD_PRODUCT;
				}
				}
				if (action.equalsIgnoreCase("edit")) {
					String name = request.getParameter("p_number");
//					name = new String(name.getBytes(), "ISO-8859-1");
//					System.out.println(name);
					Product p = dao.getProductById(name);
					request.setAttribute("p", p);
					forward = EDIT_PRODUCT;

				}
		}
				List<Product> products = dao.getAll();
				request.setAttribute("products", products);
				RequestDispatcher dispatcher = request
						.getRequestDispatcher(forward);
				dispatcher.forward(request, response);

//		String action = request.getParameter("action");
//		if (action.equalsIgnoreCase("edit")) {
//			Product p = new Product(request.getParameter("p_number"),
//					request.getParameter("p_desc"),
//					request.getParameter("p_name"),
//					request.getParameter("p_class"), Integer.parseInt(request
//							.getParameter("price")), Integer.parseInt(request
//							.getParameter("selling")), Integer.parseInt(request
//							.getParameter("stock_qty")),
//					Integer.parseInt(request.getParameter("loan_qty")),
//					Integer.parseInt(request.getParameter("usable")),
//					request.getParameter("maping"));
//			dao.update(p);
//			List<Product> products = dao.getAll();
//			request.setAttribute("products", products);
//			RequestDispatcher dispatcher = request
//					.getRequestDispatcher(LIST_PRODUCT);
//			dispatcher.forward(request, response);
//	}

	



	}
}


